package com.hua.nailcampus.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.nailcampus.acl.mapper.CityMapper;
import com.hua.nailcampus.acl.mapper.ProvinceMapper;
import com.hua.nailcampus.acl.model.City;
import com.hua.nailcampus.acl.model.Province;
import com.hua.nailcampus.acl.model.vo.ProvinceCityVo;
import com.hua.nailcampus.acl.service.ProvinceService;
import com.hua.nailcampus.constant.RedisConst;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author:chenjianhua
 * @Date:2023/11/13 19:57
 * @Version 17
 */
@Service
public class ProvinceServiceImpl extends ServiceImpl<ProvinceMapper, Province>
        implements ProvinceService {

    @Autowired
    private ProvinceMapper provinceMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private CityMapper cityMapper;
    @Override
    public Province getByName(String name) {
        LambdaQueryWrapper<Province> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Province::getName,name);
        return provinceMapper.selectOne(queryWrapper);
    }

    @Override
    public List<ProvinceCityVo> getAllProvince() {
        List<Province> provinceList = provinceMapper.selectList(null);
        List<ProvinceCityVo> provinceCityVoList = new ArrayList<>();
        for (Province province : provinceList){
            ProvinceCityVo provinceCityVo = new ProvinceCityVo(province);
            List<City> cityList = cityMapper.selectList(new LambdaQueryWrapper<City>()
                    .eq(City::getPId,province.getId()));
            provinceCityVo.setCityList(cityList);
            provinceCityVoList.add(provinceCityVo);
        }
//        存入Redis
        redisTemplate.opsForValue().set(RedisConst.PROVINCE_LISTK,provinceCityVoList,RedisConst.PROVINCES_TIMEOUT, TimeUnit.MINUTES);
        return provinceCityVoList;
    }
}
